package com.example.coreshopapp.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.coreshopapp.R;
import com.example.coreshopapp.adapter.OrderDetailAdapter;
import com.example.coreshopapp.app.Constants;
import com.example.coreshopapp.base.BaseActivity;
import com.example.coreshopapp.bean.OrderDetailData;
import com.example.coreshopapp.bean.OrderListBean;
import com.example.coreshopapp.databinding.ActivityOrderDetailBinding;
import com.example.coreshopapp.task.AllCheckoutTask;
import com.example.coreshopapp.task.OrderDetailTask;
import com.example.coreshopapp.utils.LoadingUtils;

import java.util.HashMap;

@SuppressLint("SetTextI18n")
public class OrderDetailActivity extends BaseActivity {
    private ActivityOrderDetailBinding binding;
    private OrderDetailTask orderDetailTask;
    private OrderDetailData orderDetail;
    private OrderDetailAdapter orderDetailAdapter;
    private String orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        binding = ActivityOrderDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        hideTopBar();
        initView();
        viewClick();
        getQuery();
    }

    private void getQuery() {
        Bundle extras = getIntent().getExtras();
        orderId = extras.getString("orderId");
        if (orderId != null) {
            HashMap<String, String> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("orderId", orderId);
            getOrderDetail(objectObjectHashMap);
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler orderDetailHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            LoadingUtils.hideProgressDialog();
            switch (msg.what) {
                case 9900:
                    Log.d("handleMessage: ", msg.obj.toString());
                    orderDetail = JSON.parseObject(msg.obj.toString(), OrderDetailData.class);
                    success();
                    break;
                case 9901:
                    Toast.makeText(OrderDetailActivity.this, "获取失败：" + msg.obj, Toast.LENGTH_LONG).show();
                    break;
                case 9902:
                    Toast.makeText(OrderDetailActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
                    break;
                case 9904:
                    gotoActivityFinish(LoginActivity.class);
                    Toast.makeText(OrderDetailActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
                    break;
            }

        }
    };

    @SuppressLint("HandlerLeak")
    private Handler allCheckoutHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            LoadingUtils.hideProgressDialog();
            switch (msg.what) {
                case 9900:
                 gotoActivityFinish(OrderFormActivity.class);
                    Toast.makeText(OrderDetailActivity.this, "退单成功", Toast.LENGTH_LONG).show();

                    break;
                case 9901:
                    Toast.makeText(OrderDetailActivity.this, "请求失败：" + msg.obj, Toast.LENGTH_LONG).show();
                    break;
                case 9902:
                    Toast.makeText(OrderDetailActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
                    break;
                case 9904:
                    gotoActivityFinish(LoginActivity.class);
                    Toast.makeText(OrderDetailActivity.this, msg.obj.toString(), Toast.LENGTH_LONG).show();
                    break;
            }

        }
    };

    private void initView() {
        orderDetailAdapter = new OrderDetailAdapter();
        binding.goodsList.setAdapter(orderDetailAdapter);
    }

    private void viewClick() {
        binding.allCheckOut.setOnClickListener(view -> {
            String classBillNo = Constants.AppConstVars.classBillNo;
            if (classBillNo == null || classBillNo.equals("")) {
                showtoast(this, "请先当班");
                return;
            }
            new AlertDialog.Builder(this).setTitle("温馨提示").setMessage("是否退单？")
                    .setNegativeButton("取消", null)
                    .setPositiveButton("确定", (dialogInterface, i) -> {
                        LoadingUtils.showProgressDialog(OrderDetailActivity.this, "温馨提示", "加载中，请稍后");
                        HashMap<String, String> obj = new HashMap<>();
                        obj.put("classBillNo", classBillNo);
                        obj.put("orderId", orderId);
                        AllCheckoutTask allCheckoutTask = new AllCheckoutTask(obj);
                        allCheckoutTask.setOutHandler(allCheckoutHandler);
                        allCheckoutTask.execute();
                    }).show();

        });
    }

    public void getOrderDetail(HashMap<String, String> obj) {
        LoadingUtils.showProgressDialog(OrderDetailActivity.this, "温馨提示", "加载中，请稍后");
        orderDetailTask = new OrderDetailTask(obj);
        orderDetailTask.setOutHandler(orderDetailHandler);
        orderDetailTask.execute();
    }

    private void success() {
        binding.tvOrderId.setText(orderDetail.getOrderId());
        binding.tvPerson.setText(orderDetail.getCreator() + "");
        binding.tvQuantity.setText(orderDetail.items.size() + "");
        binding.tvPayTime.setText(orderDetail.getPaymentTime());
        binding.idTvAmount.setText(orderDetail.getOrderAmount() + "");
        binding.idTvAcamount.setText(orderDetail.getPayedAmount() + "");
        binding.idTvChangeAmount.setText(orderDetail.getGiveChangeAmount() + ""); // 找零金额
        orderDetailAdapter.updateOrderDetailAdapter(orderDetail.getItems());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}